package fileshare.control;

import java.util.Timer;
import java.util.TimerTask;

import fileshare.fileprocessing.log;
import fileshare.network.peerProcess;
class timert extends TimerTask {
	public int type=1;
	
	   public void run() {
		   if(peerProcess.ifallpeerfi())
		   { this.cancel();
		   log.writelog("timer is canceled");
		   }
			   
		   
		   if(type==1)//for preferred peers
		   {
			   try{
			   updateprefer();
			   }catch (Exception e){e.printStackTrace();}
			   
		   }
		   else if(type==2)//for optmesitc
		   {
			   try{
			   updateopt();
			   }catch(Exception e){e.printStackTrace();}
			   
		   }
	   //  System.out.println("first timer");
	   }
	   public timert(int type)
	   {
		   super();
		  this.type=type;
	   }
	   public timert()
	   {}
	   private static void updateprefer()throws Exception
	   {
		   
		   //System.out.println("begin to call updateprefer function in timerthread");
		   peerProcess.prq.updateprefer();
		   
		   
	   }
	   private static void updateopt()throws Exception
	   {
		  // System.out.println("begin to call updateopt function in timerthread");
		   peerProcess.prq.updateopt();
	   }
	}
public class timerthread { 
	 /**
	  * @param args
	  */
	public timerthread(int type,int time)
	{
		 Timer timer = new Timer();
		  timer.schedule(new timert(type),0,time*1000); 
		
	}
	 /*public static void main(String[] args) {
	  timerthread time=new timerthread(2,15);
	  timerthread tim=new timerthread(1,5);
	 }*/
	 
	 }

	



